package com.easyedu.dean.controller;

import com.easyedu.core.entity.po.BuildingAddress;
import com.easyedu.core.util.R;
import com.easyedu.dean.entity.dto.request.ReqCreateClassroom;
import com.easyedu.dean.entity.dto.request.ReqUpdateClassRoom;
import com.easyedu.dean.service.ClassRoomService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author lmr </br>
 * 教室管理
 **/
@Validated
@RestController
@RequestMapping("/dean")
public class ClassRoomController {

    private final ClassRoomService classRoomService;

    public ClassRoomController(ClassRoomService classRoomService) {
        this.classRoomService = classRoomService;
    }

    /**
     * 功能描述:获取所有教室信息
     *
     * @return
     */
    @GetMapping("/build")
    public R getBuild() {
        List<BuildingAddress> buildingAddressList = classRoomService.getBuild();
        return R.ok().data(buildingAddressList);
    }

    /**
     * 功能描述: 获取所有教室
     *
     * @return : com.easyedu.api.util.R
     **/
    @GetMapping("/classroom")
    public R getClassroom() {
        return R.ok().data(classRoomService.getClassroom());
    }

    /**
     * 功能描述: 创建教室
     *
     * @param reqCreateClassrooms 1 教室创建集  Data Transfer Object
     * @return : com.easyedu.api.util.R
     **/
    @PostMapping("/classroom")
    public R createClassroom(@RequestBody @Valid List<ReqCreateClassroom> reqCreateClassrooms) {
        List<String> classroom = classRoomService.createClassroom(reqCreateClassrooms);
        if (classroom.size() == 0) {
            return R.ok().message("成功创建" + reqCreateClassrooms.size() + "个教室。");
        }
        return R.ok().message("成功创建" + (reqCreateClassrooms.size() - classroom.size()) + "个教室,有" + classroom.size() +
                "因已存在同名教室无法创建").data(classroom);
    } 

    /**
     * 功能描述: 修改教室信息
     *
     * @param classroom 1 教室修改集 Data Transfer Object
     * @return : com.easyedu.api.util.R
     **/
    @PutMapping("/classroom")
    public R updateClassroom(@RequestBody @Valid ReqUpdateClassRoom classroom) {
        classRoomService.updateClassroom(classroom);
        return R.ok();
    }

    /**
     * 功能描述:删除教室
     *
     * @param classroomId 1 教室id
     * @return : com.easyedu.api.util.R
     **/
    @DeleteMapping("/classroom")
    public R deleteClassroom(@RequestParam Long classroomId) {
        classRoomService.deleteClassroom(classroomId);
        return R.ok();
    }
}
